<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.util.*" import="cart.hibernate.releaseCompany.*, cart.hibernate.movieInformation.*" import="cart.*,java.net.*,java.text.*" %>
<%@page import="cart.hibernate.coverImage.*, cart.hibernate.rating.*, cart.hibernate.order.*, cart.hibernate.genre.*, cart.hibernate.actor.*" %> 
<jsp:useBean id="movieInformationManager" scope="request" class="cart.hibernate.movieInformation.ManageMovieInformation" />

<%
    int id = Integer.parseInt(request.getParameter("id"));
    SimpleDateFormat formatter=new SimpleDateFormat("dd-MM-yyyy");

    List movieInformationList = movieInformationManager.getAllMovie();
    //List orderList = orderManager.getAllOrder();

    Iterator iterMovieInformation = movieInformationList.iterator();
    
    while (iterMovieInformation.hasNext())
    {
        MovieInformation movieInformation = (MovieInformation) iterMovieInformation.next();
        if((movieInformation.getMovieId() - id + 1) < 1)
                continue;
%>
<!DOCTYPE html>
<html>
    <head>
        <title><%=movieInformation.getMovieName()%></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script type="text/javascript" src="js/global.js"></script>
        <script type="text/javascript" src="js/jquery.colorbox-min.js"></script>
        <link type="text/css" href="css/global.css" rel="stylesheet" />
        <link type="text/css" href="css/colorbox.css" rel="stylesheet" />
        <script>
        //rating
        $(document).ready(function() {

                $('.rate_widget').each(function(i) {
                    var widget = this;
                    set_votes(widget, <%=(int)(movieInformation.getRating().getAvgRate()/2)%>);
                });


                $('.ratings_stars').hover(
                    // Handles the mouseover
                    function() {
                        $(this).prevAll().andSelf().addClass('ratings_over');
                        $(this).nextAll().removeClass('ratings_vote'); 
                    },
                    // Handles the mouseout
                    function() {
                        $(this).prevAll().andSelf().removeClass('ratings_over');
                        // can't use 'this' because it wont contain the updated data
                        set_votes($(this).parent());
                    }
                );


                // This actually records the vote
                $('.ratings_stars').bind('click', function() {
                    var star = this;
                    var widget = $(this).parent();

                    var clicked_data = {
                        movieid: <%=movieInformation.getMovieId()%>,
                        rate: $(star).attr('data'),
                    };
                    $.post(
                        'rating.jsp',
                        clicked_data,
                        function(INFO) {
                            set_votes(widget, INFO.avg);
                        },
                        'json'
                    ); 
                });
                
                $("#purchase").click(function(e) {
                    e.preventDefault();
                    if(!localStorage.username)
                        $(this).html("Please Login First");
                    $.get("purchase.jsp", {
                        movieid: <%=movieInformation.getMovieId()%>,
                        userid: localStorage.username
                    }, function(data) {
                        if(data.success)
                            $(this).html("Added");
                        else
                            $(this).html("Fail, please try again later");
                    }, "json");
                });
            });

            function set_votes(widget, avg) {
                $(widget).find('.star_' + avg).prevAll().andSelf().addClass('ratings_vote');
                $(widget).find('.star_' + avg).nextAll().removeClass('ratings_vote'); 
            }
        </script>
        <style type="text/css">
            body {
                background: #FFF;
            }
            .rate_widget {
                overflow:   visible;
                padding:    10px;
                position:   relative;
                width:      180px;
                height:     32px;
            }
            .ratings_stars {
                background: url('img/star_empty.png') no-repeat;
                float:      left;
                height:     28px;
                padding:    2px;
                width:      32px;
            }
            .ratings_vote {
                background: url('img/star_full.png') no-repeat;
            }
            .ratings_over {
                background: url('img/star_highlight.png') no-repeat;
            }
            .total_votes {
                background: #eaeaea;
                top: 58px;
                left: 0;
                padding: 5px;
                position:   absolute;  
            } 
            .rating_style {
                font: 10px verdana, sans-serif;
                margin: 0 auto 40px auto;
                width: 180px;
            }
        </style>
    </head>
    <body>
        <div style="position: absolute; top: 8px; right: 50%; margin-right: 5px; display: inline-block; padding: 5px 9px; color: #FFF; background: rgba(0, 0, 0, .65); font-size: 40px; border-radius: 0 15px; z-index: 999"><%=movieInformation.getMovieName()%></div>
        <img src="img/<%=movieInformation.getMovieId()%>/cover.jpg" style="position: absolute; width: 50%; height: auto" />
        <div style="position: absolute; top: 10px; right: 0; left: 50%; margin-left: 10px">
            Description:<br />
            <%=movieInformation.getProductDescription()%><br /><br />
            Price:<br />
            <%=movieInformation.getUnitPrice()%><br /><br />
            Stock:<br />
            <%=movieInformation.getStockQuantity()%><br /><br />
            Release Date:<br />
            <%=formatter.format(movieInformation.getReleaseDate())%><br /><br />
            Available Date:<br />
            <%=formatter.format(movieInformation.getProductAvailableDate())%><br /><br />
            Rating:<br />
            <div id="rating" class="rate_widget">
                <div class="star_1 ratings_stars" data="1"></div>
                <div class="star_2 ratings_stars" data="2"></div>
                <div class="star_3 ratings_stars" data="3"></div>
                <div class="star_4 ratings_stars" data="4"></div>
                <div class="star_5 ratings_stars" data="5"></div>
            </div>
            <div>
                <img src="img/<%=movieInformation.getMovieId()%>/1.jpg" style="width: 30%; height: auto; margin: 10px 5%" />
                <img src="img/<%=movieInformation.getMovieId()%>/2.jpg" style="width: 30%; height: auto; margin: 10px 5%" />
            </div>
        </div>
        <a id="purchase" href="#" class="btn btn-highlight" style="position: fixed; top: 0; right: 5px">Add to Cart</a>
    </body>
</html>
<%
        break;
    }
%>
